package cc.comac.mom.datahandlers.entity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cc.comac.mom.bean.QualityMaterialData;
import cc.comac.mom.dao.entity.QualityMaterialDataDao;
import cc.comac.mom.entity.QualityMaterialDataEntity;
import cc.comac.mom.util.BaseEntityDataUtil;

@Component
@Transactional
public class QualityMaterialDataDataHandler {
	@Autowired
	private QualityMaterialDataDao QualityMaterialDataDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	public int insert(List<QualityMaterialData> QualityMaterialDataList) {
		// 保存数据
		if(QualityMaterialDataList==null || QualityMaterialDataList.size()<1) {
			throw new RuntimeException("未检测到物料主数据！");
		}
		for(QualityMaterialData QualityMaterialData:QualityMaterialDataList)
		{
			baseEntityDataUtil.setBaseEntityData(QualityMaterialData);
		}
		Integer insert = QualityMaterialDataDao.insertMaterialBatch(QualityMaterialDataList);
		return insert;
	}
	/**
	 * 分页查询
	 * 
	 * @param equipParamItem
	 * @return
	 */
	public QualityMaterialData selectAllByPage(QualityMaterialData QualityMaterialData) {
		QualityMaterialData returnQualityMaterialData = new QualityMaterialData();
		if(QualityMaterialData.getPageNum()==0) {
			QualityMaterialData.setPageNum(1);
		}
		if(QualityMaterialData.getPageSize()==0) {
			QualityMaterialData.setPageSize(10);
		}
		
		QueryWrapper<QualityMaterialDataEntity> queryWrapper = new QueryWrapper<QualityMaterialDataEntity>();
		queryWrapper.eq("row_status", 1);
		if(QualityMaterialData.getMatnr()!=null && !"".equals(QualityMaterialData.getMatnr())) {
			queryWrapper.like("matnr", QualityMaterialData.getMatnr());
		}
		if(QualityMaterialData.getMaktx()!=null && !"".equals(QualityMaterialData.getMaktx())) {
			queryWrapper.like("maktx", QualityMaterialData.getMaktx());
		}
		if(QualityMaterialData.getMtart()!=null && !"".equals(QualityMaterialData.getMtart())) {
			queryWrapper.like("mtart", QualityMaterialData.getMtart());
		}
		// 不返回总记录数 设置false
		Page<QualityMaterialDataEntity> page = new Page<>(QualityMaterialData.getPageNum(), QualityMaterialData.getPageSize(), true);
		IPage<QualityMaterialDataEntity> iPage = QualityMaterialDataDao.selectPage(page, queryWrapper);
		System.out.println("总页数:" + iPage.getPages());
		System.out.println("总记录数:" + iPage.getTotal());
		List<QualityMaterialDataEntity> records = iPage.getRecords();
		returnQualityMaterialData.setPageNum(QualityMaterialData.getPageNum());
		returnQualityMaterialData.setPageSize(QualityMaterialData.getPageSize());
		returnQualityMaterialData.setEntityList(records);
		returnQualityMaterialData.setTotalQuantity((int) iPage.getTotal());
		returnQualityMaterialData.setPageCount((int) iPage.getPages());
		return returnQualityMaterialData;
	}
}
